package com.crpc.core;

import com.crpc.core.discovery.Registry;
import com.crpc.core.proxy.handler.RpcConsumerInvocationHandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author CLOUD
 * @version 1.0
 * @description:
 * @date 2023/9/7 14:19
 */
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ReferenceConfig<T> {
    private Class<T> interfaceProvider;
    private Object ref;
    private Registry registry;

    public T get() {
        //动态代理生成对象
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Class[] classes = new Class[]{interfaceProvider};
        InvocationHandler invocationHandler = new RpcConsumerInvocationHandler(registry, interfaceProvider);
        //使用动态代理
        Object helloProxy = Proxy.newProxyInstance(
                classLoader,
                classes,
                invocationHandler);
        return (T) helloProxy;
    }
}
